package org.tartarus.snowball;

import java.util.Vector;
import java.util.Enumeration;

import java.lang.reflect.Method;

public class Stemming{

	//** Atributos **/////////////////////////////////////////////
	private SnowballProgram spanishStemmer;
	private SnowballProgram englishStemmer;
	
	private Method spanishStemMethod;
	private Method englishStemMethod;
	
	private Vector lyrics;
	private Vector stemmedLyrics;
	
	//////////////////////////////////////////////////////////////
	
	//** Metodos **////////////////////////////////////////////////
	
	//Constructor
	public Stemming(Vector _lyrics){
		
		lyrics =  _lyrics;
		stemmedLyrics = new Vector();
		
		try{
			
			init();
		}
		catch(Throwable e){
		
			System.err.println("Can't find " + e + "class.");
		}
	}

	//Getter
	public Vector getStemmedLyrics(){
		
		return stemmedLyrics;
	}
	
	//Inicia algunas variables
	private void init()throws Throwable{
	
		Class spanishStemClass = Class.forName("org.tartarus.snowball.ext.spanishStemmer");
		spanishStemmer = (SnowballProgram) spanishStemClass.newInstance();
		
		spanishStemMethod = spanishStemClass.getMethod("stem", new Class[0]);
		
		Class englishStemClass = Class.forName("org.tartarus.snowball.ext.englishStemmer");
		englishStemmer = (SnowballProgram) englishStemClass.newInstance();
		
		englishStemMethod = englishStemClass.getMethod("stem", new Class[0]);
	}
	
	//Metodo que controla todo el proceso de stemming, y llena el vector
	//stemmedLyrics con vectores con la misma estructura que el vector lyrics
	//pero ya con la letra de la cancion tratada.
	public void doStemming(){
		
		Vector lyricData;
		Vector result = new Vector();
		
		String language, lyric;
		
		String stemmedWord = new String();
		String stemmedLyric = new String();
		
		Enumeration e = lyrics.elements();
		
		StringBuffer input = new StringBuffer();
		Object [] emptyArgs = new Object[0];
		int character, indexChar=0;
		
		
		while(e.hasMoreElements()){
			
			lyricData = (Vector)e.nextElement();
			
			result.add(lyricData.firstElement());
			
			language = (String)lyricData.elementAt(1);
			result.add(language);
			
			lyric = (String)lyricData.lastElement();

			//***********************************************
			while(indexChar<lyric.length()){

				character = lyric.charAt(indexChar);
				
			    char ch = (char) character;

			    if(Character.isWhitespace((char) ch)){
				
					if(input.length() > 0){
				    
						if(language.equals("spanish")){
							
							try{	
								
								spanishStemmer.setCurrent(input.toString());
								spanishStemMethod.invoke(spanishStemmer, emptyArgs);
								stemmedWord = spanishStemmer.getCurrent();
							}
							catch(Throwable e1){
							
								System.err.println("Can't find " + e1 + "class.");
							}
						}
						else{
							
							try{								
								englishStemmer.setCurrent(input.toString());
								englishStemMethod.invoke(englishStemmer, emptyArgs);
								stemmedWord = englishStemmer.getCurrent();
							}
							catch(Throwable e2){
							
								System.err.println("Can't find " + e2 + "class.");
							}
						}
						
						stemmedLyric = stemmedLyric + stemmedWord + " ";
						
						input.delete(0, input.length());
					}
			    }
				else{
						
					input.append(Character.toLowerCase(ch));
			    }

			    indexChar++;
			}
			//***********************************************
			stemmedLyric = stemmedLyric.substring(0, stemmedLyric.length() - 1);
			result.add(stemmedLyric);

			stemmedLyrics.add(result);
			
			indexChar = 0;
			stemmedLyric = new String();
			result = new Vector();
		}
	}
	
	//////////////////////////////////////////////////////////////
}
